package main

import (
	"GoodSrv/common/global"
	"GoodSrv/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"log"
	"os"
	"time"
)

// 生成数据表的脚本
func main() {
	dsn := fmt.Sprintf("root:123456@tcp(127.0.0.1:3366)/zg5_demo?charset=utf8mb4&parseTime=True&loc=Local")
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second,   // 慢 SQL 阈值
			LogLevel:      logger.Silent, // Log level
			Colorful:      true,          // 禁用彩色打印
		},
	)

	// 全局模式
	var err error
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: newLogger,
	})
	if err != nil {
		panic(err)
	}
	//表迁移
	_ = global.DB.AutoMigrate(&model.Goods{}, &model.PicFiles{}, &model.Classification{}, &model.Brands{})

}
